'''
Created on 30/04/2012

@author: Bruna, Bruno
'''

from data_extractor import DataExtractor
from color_helper import calculate_rgb_normalized
import sys

class SampleAnalyser:
    def __init__(self, files):
        self.histograms = {}
        for file_name in files:
            dataExtractor = DataExtractor(file_name)
            dataExtractor.extract_data()
            
            self.histograms[file_name] = dataExtractor.histogram
    
    def __get_probabilities(self, (r, g, b)):
        normalized_sample = calculate_rgb_normalized(r, g, b)
        #print "Norm. Sample:", normalized_sample
        
        probabilities = {}
        for histogram in self.histograms.keys():
            probabilities[histogram] = self.histograms[histogram].probability(normalized_sample)
        #print "Probs.:", probabilities
        return probabilities
        
    def analyse_samples(self, samples):
        #print "Samples:",samples
        probabilities = {}
        for sample in samples:
            self.__update(probabilities, self.__get_probabilities(sample))
        return probabilities
        
    def __update(self, probabilities, new_probabilities):
        for key in new_probabilities.keys():
            if probabilities.has_key(key):
                probabilities[key] += new_probabilities[key]
            else:
                probabilities[key] = new_probabilities[key]